package logger

import (
	"go.uber.org/zap"
)

func New(config *Config, opts ...zap.Option) *zap.Logger {

	zapConfig := zap.Config{
		Level:            config.Level,
		Development:      false,
		Encoding:         "json",
		OutputPaths:      []string{"stderr"},
		ErrorOutputPaths: []string{"stderr"},
	}

	if config.Development {
		zapConfig.EncoderConfig = zap.NewDevelopmentEncoderConfig()
		zapConfig.Development = true
		zapConfig.Encoding = "console"
	} else {
		zapConfig.EncoderConfig = zap.NewProductionEncoderConfig()
		zapConfig.Sampling = &zap.SamplingConfig{
			Initial:    100,
			Thereafter: 100,
		}
	}

	logger, err := zapConfig.Build(opts...)

	if err != nil {
		panic(err)
	}

	return logger
}
